﻿using System;
using System.Data;
using Generico.Coneccion;

using System.Collections.Generic;

using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Services;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using SelectPdf;


namespace RosarioFinanzas
{
    public class ViewNoticia 
    {
        public string resultado {get;set;}
        public string url {get;set;}
        public string Titulo {get;set;}
        public string Copete {get;set;}
    }

    public partial class Noticia : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = 0;

            try
            {
                var noticias = new WebRosarioFinanzas.Service1().GetNoticiaRow(Page.Request.QueryString["id"].ToString());

                //Add Meta Tag
                HtmlMeta title = new HtmlMeta();
                title.HttpEquiv = "title";
                title.Name = "title";
                title.Content = Regex.Replace(noticias["Titulo"].ToString(), "<.*?>", string.Empty).Replace("\n", "").Replace("\r", "").Replace("\t", "");
                this.Page.Header.Controls.Add(title);
                
                //Add Description Meta Tag
                HtmlMeta description = new HtmlMeta();
                description.HttpEquiv = "description";
                description.Name = "description";
                description.Content = Regex.Replace(noticias["Resumen"].ToString(), "<.*?>", string.Empty).Replace("\n", "").Replace("\r", "").Replace("\t", "");
                this.Page.Header.Controls.Add(description);

                // Social Media Tags
                HtmlMeta tagOgTitle = new HtmlMeta();
                tagOgTitle.Attributes.Add("property", "og:title");
                tagOgTitle.Content = Regex.Replace(noticias["Titulo"].ToString(), "<.*?>", string.Empty).Replace("\n", "").Replace("\r", "").Replace("\t", "");
                Page.Header.Controls.Add(tagOgTitle);

                HtmlMeta tagOgDescription = new HtmlMeta();
                tagOgDescription.Attributes.Add("property", "og:description");
                tagOgDescription.Content = Regex.Replace(noticias["Resumen"].ToString(), "<.*?>", string.Empty).Replace("\n", "").Replace("\r", "").Replace("\t", "");
                Page.Header.Controls.Add(tagOgDescription);

                HtmlMeta tagOgImage = new HtmlMeta();
                tagOgImage.Attributes.Add("property", "og:image");
                tagOgImage.Content = "http://rosariofinanzas.com.ar/Imagenes/Cabecera_FB.png";
                Page.Header.Controls.Add(tagOgImage);

                HtmlMeta tagOgUrl = new HtmlMeta();
                tagOgUrl.Attributes.Add("property", "og:url");
                tagOgUrl.Content = "http://rosariofinanzas.com.ar/Noticia.aspx?id=" + Page.Request.QueryString["id"].ToString();
                Page.Header.Controls.Add(tagOgUrl);

            }
            catch (Exception)
            {
                Response.Redirect("~/Noticias.aspx");
            }
        }
        [WebMethod]
        public static string strFecha(string str)
        {

            string[] dayarray = { "Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado" };
            string[] montharray = { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" };
            var daym = DateTime.Today.Day.ToString();
            var year = DateTime.Today.Year.ToString();
            var day = Convert.ToInt32(DateTime.Today.DayOfWeek);
            var month = DateTime.Today.Month - 1;
            return dayarray[day] + ", " + daym + " de " + montharray[month] + " del " + year + " ";

        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<ViewNoticia> strNoticia(string str)
        {

            var cn = new ClsConeccion();
            cn.Abrir();
            string strsql = "Select Titulo,Resumen, Fecha, HTML, face, twitter From Noticias where Id = " + str;
            var rd = cn.EjecutarSqlDataReader(strsql);
            var dt = new DataTable();
            dt.Load(rd);
            cn.Cerrar();
            string html = "";

            var fecha = Convert.ToDateTime(dt.Rows[0]["Fecha"]);

            var objNoticia = new ViewNoticia ();
            if (dt.Rows.Count > 0)
            {
                objNoticia.Titulo = dt.Rows[0]["Titulo"].ToString() ;
                objNoticia.Copete =  dt.Rows[0]["Resumen"].ToString() ;

                html += "<div class='BarraSuperior'><div class='ArtFecha' style=\"margin-bottom: 10px;\"><B>" + fecha.Day + "</B> <font style=\"font-size: 11px;\"> " + GetMes(fecha.Month.ToString()) + "</font> " + fecha.Year + "</div></div><br/>";
                
                html += "<div class='Titulo'>" + dt.Rows[0]["Titulo"].ToString() + "</div>";
                html += "<div class='Cuerpo'>" + dt.Rows[0]["HTML"].ToString() + "</div>";
                
           //     var pop = "onclick=\"window.open(this.href, \'mywin\',\'left=20,top=20,width=500,height=500,toolbar=1,resizable=0\'); return false;\"";
            //    html += "<div class='BarraInferior'>";
           //     html += "<span class='BISpan'><a id=\"face\" href=\"javascript:fb('" + dt.Rows[0]["Titulo"].ToString() + "','" + dt.Rows[0]["Resumen"].ToString() + "')\" ><img src='Imagenes/fgris.png' /></a></span>";
           //     html += "<span class='BISpan'><a href=\"" + dt.Rows[0]["twitter"].ToString() +"\" " + pop + "><img src='Imagenes/tgris.png' /></a></span>";

          //      html += "<div class=\"fb-share-button\" data-href=\"\" data-layout=\"button_count\"></div>   <a href=\"https://twitter.com/share\" class=\"twitter-share-button\" data-url=\"\" data-via=\"rosariofinanzas\" data-lang=\"es\" data-text=\"\">Twittear</a> ";

         //       html += "</div>";
            }
         
            objNoticia.resultado = html;

            var Col = new List<ViewNoticia>();
            Col.Add(objNoticia);


            return Col;

        }


        protected void ConvertirHTMLtoPDF(object sender, EventArgs e)
        {
            try
            {

                // read parameters from the webpage
                string url = Request.Url.AbsoluteUri;

                PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), "A4", true);

                PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), "Portrait", true);

                int webPageWidth = 1024;
                try
                {
                    webPageWidth = Convert.ToInt32("1024");
                }
                catch { }

                int webPageHeight = 0;
                try
                {
                    webPageHeight = Convert.ToInt32("768");
                }
                catch { }

                // instantiate a html to pdf converter object
                HtmlToPdf converter = new HtmlToPdf();

                // set converter options
                converter.Options.PdfPageSize = pageSize;
                converter.Options.PdfPageOrientation = pdfOrientation;
                converter.Options.WebPageWidth = webPageWidth;
                converter.Options.WebPageHeight = webPageHeight;
                converter.Options.MarginTop = 10;
                converter.Options.MarginLeft = 10;
                converter.Options.MarginRight = 10;
                converter.Options.MarginBottom = 10;


                // create a new pdf document converting an url
                PdfDocument doc = converter.ConvertUrl(url);

                // save pdf document
                doc.Save(Response, false, "Noticia.pdf");

                // close pdf document
                doc.Close();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private static string GetMes(string num)
        {

            switch (num)
            {
                case "1": return "ENERO";
                case "2": return "FEBRERO";
                case "3": return "MARZO";
                case "4": return "ABRIL";
                case "5": return "MAYO";
                case "6": return "JUNIO";
                case "7": return "JULIO";
                case "8": return "AGOSTO";
                case "9": return "SEPTIEMBRE";
                case "10": return "OCTUBRE";
                case "11": return "NOVIEMBRE";

                default: return "DICIEMBRE";
            }     
        
        }

    }
}
